﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WIKID.Controller;
using WIKID.Common;

namespace WIKID
{
    public partial class frmParentLogin : Form
    {
        private int offSet = 120;

        private bool changePassStatus = false;

        private ParentController parentController;

        public frmParentLogin()
        {
            InitializeComponent();
            parentController = new ParentController();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string strUsername = txtUsername.Text.Trim();
            string strPassword = txtPassword.Text.Trim();
            
                if (parentController.CheckPass(strUsername, strPassword) == Constants.LOGIN_RESULT.InvalidID)
                    MessageBox.Show(Constants.INVALID_USERNAME[0], Constants.INVALID_USERNAME[1]);
                else if (parentController.CheckPass(strUsername, strPassword) == Constants.LOGIN_RESULT.InvalidPassword)
                    MessageBox.Show(Constants.INVALID_PASSWORD[0], Constants.INVALID_PASSWORD[1]);
                else
                {
                    if (changePassStatus)
                    {
                        string strNewUsername = txtNewUsername.Text.Trim();
                        string strNewPassword = txtNewPassword.Text.Trim();
                        string strConfirmPassword = txtConfirmPassword.Text.Trim();
                        if (strNewUsername.Equals(string.Empty))
                            MessageBox.Show(Constants.BLANK_USERNAME[0],Constants.BLANK_USERNAME[1]);
                        else if (strNewPassword.Equals(string.Empty))
                            MessageBox.Show(Constants.BLANK_PASSWORD[0],Constants.BLANK_PASSWORD[1]);
                        else if (!strConfirmPassword.Equals(strNewPassword))
                            MessageBox.Show(Constants.PASSWORD_NOT_MATCH[0], Constants.PASSWORD_NOT_MATCH[1]);
                        else
                        {
                            parentController.ChangePass(strNewUsername, strNewPassword);
                            MessageBox.Show(Constants.CHANGE_PASS_SUCCESS[0], Constants.CHANGE_PASS_SUCCESS[1]);
                            gbChangePass.Enabled = false;
                            gbChangePass.Visible = false;
                            btnLogin.Text = "Login";
                            lblChangePass.Enabled = true;
                            btnLogin.Location = new Point(btnLogin.Location.X, btnLogin.Location.Y - offSet);
                            btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y - offSet);
                            this.Height -= offSet;
                            this.changePassStatus = false;
                        }
                    }
                    else
                    {
                        frmParent.current.Enabled = true;
                        this.Close();
                    }
                }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (changePassStatus)
            {
                gbChangePass.Enabled = false;
                gbChangePass.Visible = false;
                btnLogin.Text = "Login";
                lblChangePass.Enabled = true;
                btnLogin.Location = new Point(btnLogin.Location.X, btnLogin.Location.Y - offSet);
                btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y - offSet);
                this.Height -= offSet;
                this.changePassStatus = false;

            }
            else
            {
                this.Close();
                if (frmParent.current != null)
                {
                    frmParent.current.Close();
                }
                if (frmLogin.current != null)
                {
                    frmLogin.current.Show();
                }
            }
        }

        private void frmParentLogin_Load(object sender, EventArgs e)
        {
            if (frmParent.current == null)
            {
                frmParent parentForm = new frmParent();
                parentForm.Show();
            }
        }

        private void lblChangePass_Click(object sender, EventArgs e)
        {
           
            gbChangePass.Enabled = true;
            gbChangePass.Visible = true;
            btnLogin.Text = "Change";
            this.changePassStatus = true;
            btnLogin.Location = new Point(btnLogin.Location.X, btnLogin.Location.Y + offSet);
            btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + offSet);
            this.Height += offSet;
            lblChangePass.Enabled = false;
        }

    }
}
